<template>
  <div class="table-all">
    <!-- 搜索区 -->
    <div class="app-container search-box">
      <SearchForm
        :form="searchData.form"
        :options="searchData.options"
        @searchUpdate="searchAll"
        @resetSearchData="resetSearchData"
      ></SearchForm>
    </div>
    <div class="app-container" style="color: #606266">
      <el-row :gutter="20">
        <el-col :span="12">
        <div class="title">总体汇总情况</div>
        <el-row :gutter="10">
          <el-col :span="10" class="col">
            <div class="div_title">体检指标总数</div>
            <div class="div_num">{{countData.allNum}}</div>
          </el-col>
          <el-col :span="10" class="col">
            <div class="div_title">已完成指标数量</div>
            <div class="div_num">{{countData.completeNum}}</div>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="10" class="col">
            <div class="div_title">未完成指标数量</div>
            <div class="div_num">{{countData.incompleteNum}}</div>
          </el-col>
          <el-col :span="10" class="col">
            <div class="div_title">指标完成率</div>
            <div class="div_num">{{countData.completeRate}}</div>
          </el-col>
        </el-row>
      </el-col>
        <el-col :span="12">
          <div class="title">体检结果汇总</div>
          <el-row :gutter="10">
            <el-col :span="10" class="col">
              <div class="div_title">预警指标</div>
              <div class="div_num">{{countData.yuJingRate}}</div>
            </el-col>
            <el-col :span="10" class="col">
              <div class="div_title">一般指标</div>
              <div class="div_num">{{countData.yiBanRate}}</div>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="10" class="col">
              <div class="div_title">良好指标</div>
              <div class="div_num">{{countData.liangHaoRate}}</div>
            </el-col>
            <el-col :span="10" class="col">
              <div class="div_title">优秀指标</div>
              <div class="div_num">{{countData.youXiuRate}}</div>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </div>
    <!-- 按钮区 -->
    <div class="app-container table-box">
      <div class="table-main">
        <!-- 表格 -->
        <v-table
          title="城市自体检诊断信息"
          v-if="tableMainHeight>0"
          :table-columns="tableColumns"
          :table-data="tableData"
          :loading="loading"
          :is-index="true"
          ref="table"
          :maxHeight="tableMainHeight">
          <template #topButtonList>
            <ButtonForm :buttonList="buttonList" @clickButton="clickButton"></ButtonForm>
          </template>
          <template #checkType="scope">
            {{getDictValue(scope.row.checkType, checkTypeOptions)}}
          </template>
          <template #resultType="scope">
            {{getDictValueName(scope.row.resultType, resultTypeOptions) }}
          </template>
          <template #result="scope">
            {{ getResultLabel(scope.row)}}
          </template>
          <template #operation="scope">
            <a class="table-btn-view table-operation"
               @click="handleDetail(scope.row)">{{$t('table.view')}}</a>
          </template>
        </v-table>
      </div>
      <div class="table-page">
        <!-- 页码 -->
        <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize"
                    @pagination="search"/>
      </div>
    </div>
    <!-- 详情 -->
    <detailDialog :dialog-visible="detailDialogShow"
                  @close="editClose"
                  :openType="detailOpenType"
                  :id="detailId"
                  :title="detailTitle"
                  :showRule = 'false'
                  :cityCheckTypeOptions="checkTypeOptions"
                  :cityCheckResultTypeOptions = "resultTypeOptions"
                  :cityCheckResultDict = "cityCheckResultDict"
    ></detailDialog>
  </div>
</template>

<script>
  import { cityDiagnosis, cityDiagnosisListExport,cityWaringResultPage } from '@/api/risk/cityWaringResult'
  import { getAreaList } from '@/api/system/area'
  import SearchForm from '@/components/searchForm/index'
  import ButtonForm from '@/components/ButtonForm/index'
  import VTable from '@/components/VTable/index'
  import Pagination from '@/components/Pagination/index'
  import detailDialog from '../../cityCheck/waringResult/detail.vue'

  export default {
    components:{
      VTable,Pagination,SearchForm,ButtonForm,detailDialog
    },
    data(){
      return{
        tableColumns:[
          {
            prop: 'threeTargetName',
            label: '指标名称',
            minWidth: '150',
            slotname: 'threeTargetName',
            options: {}
          },
          {
            prop: 'checkType',
            label: '体检分类',
            minWidth: '150',
            slotname: 'checkType',
            options: {}
          },
          {
            prop: 'oneTargetName',
            label: '指标大类',
            minWidth: '150',
            slotname: 'oneTargetName',
            options: {}
          },
          {
            prop: 'twoTargetName',
            label: '指标细类',
            minWidth: '150',
            slotname: 'twoTargetName',
            options: {}
          },
          {
            prop: 'year',
            label: '年份',
            minWidth: '100',
            slotname: 'year',
            options: {}
          },
          {
            prop: 'resultType',
            label: '结果类型',
            minWidth: '120',
            slotname: 'resultType',
            options: {}
          },
          {
            prop: 'value',
            label: '指标值',
            minWidth: '120',
            slotname: 'value',
            options: {}
          },
          {
            prop: 'result',
            label: '指标结果',
            minWidth: '120',
            slotname: 'result',
            options: {}
          },
          {
            prop: 'operation',
            label: '操作',
            width: '200',
            align: 'center',
            slotname: 'operation',
            overflow: false,
            fixed: 'right'
          }
        ],
        countData:{
          allNum:"",
          completeNum:"",
          incompleteNum:"",
          completeRate:"",
          youXiuRate:"",
          liangHaoRate:"",
          yiBanRate:"",
          yuJingRate:""
        },
        tableData:[],
        selection: [],
        loading:false,
        total:1,
        pageNum:1,
        pageSize:10,
        detailOpenType:"add",
        detailDialogShow:false,
        detailId:"",
        detailTitle:"新增",
        dict:{},
        searchData:{
          form:{
           year:new Date().getFullYear()+'',
           cityCode:"440100000000"
          },
          options:[
            {
              title:'年份',
              key: "year",
              type: "dateYear",
            },
            {
              title:'城市',
              key: "cityCode",
              type: "select",
              multiple:false,
              option: {
                data: this.cityList,
                props: {
                  key: "id", // 指定key对应data的字段
                  label: "areaName", // 指定label对应data的字段
                  value: "areaCode", // 指定value对应data的字段
                },
              },
            }
          ]
        },
        buttonList:[
          {name:'导出数据',methods:'handleExport', type:'primary', hasPremission:["urbanDiagnosis:export"]}
        ],
        tableMainHeight: 0,
        // 字典
        cityList:[],
        checkTypeOptions:[],
        resultTypeOptions:[],
        cityCheckResultDict:{}
      }
    },
    watch: {
      // 字典
      'typeOptions':{
        handler(newVal, oldVal) {
          this.searchData.options.forEach(t=>{
            if(t.key=='type'){
              t.option.data = newVal
            }
          })
        },
        deep: true,
        immediate: true
      },
      'cityList':{
        handler(newVal, oldVal){
          if(newVal){
            this.searchData.options.forEach(t=>{
              if(t.key == 'cityCode'){
                t.option.data = newVal
              }
            })
          }
        }
      },
      'resultTypeOptions':{
        handler(newVal, oldVal) {
          if(newVal){
            console.log("resultTypeOptions",this.resultTypeOptions)
            this.resultTypeOptions.forEach(t=>{
              this.cityCheckResultDict[t.name] = []
              this.$commonJs.filterDataDict(t.name, this.cityCheckResultDict[t.name])
            })
          }
        },
        deep: true,
        immediate: true
      },
    },
    methods:{
      getResultLabel(row){
        return this.getDictValue(row.result, this.cityCheckResultDict[row.resultType])
      },
      getDictValueName(value,option){
        let item = option.find(t=> t.name == value)
        if(item!=undefined){
          return item.label
        }
        return value
      },
      getDictValue(value,option){
        let item = option.find(t=> t.value == value)
        if(item!=undefined){
          return item.label
        }
        return value
      },
      editClose(){
        this.detailDialogShow = false
      },
      editSuccess(){
        this.search()
        this.detailDialogShow = false
      },
      search(){
        this.fetch({
          ...this.searchData.form
        })
      },
      searchAll(){
        this.search()
        this.getCountDataInfo()
      },
      getCountDataInfo(){
        let params = {
          ...this.searchData.form,
          checkType:1
        }
        cityDiagnosis(params).then(res=>{
            if(res.code==200){
               this.countData = res.data
            }
        })
      },
      fetch(params={}){
        params.pageSize = this.pageSize
        params.pageNum = this.pageNum
        params.checkType = 1
        this.loading = true
        cityWaringResultPage(params).then(res=>{
          if(res.code==200){
            this.total = res.data.total
            this.tableData = res.data.rows
          }else{
            this.$message({
              message: this.$t('tips.systemException'),
              type: 'error'
            })
          }
        }).finally(final=>{
          this.loading = false
        })
      },
      // 重置搜索条件
      resetSearchData(){
        this.searchData.form = {
            year: new Date().getFullYear()+'',
            cityCode:"440100000000"
        }
      },
      // 触发按钮区域按钮方法
      clickButton(methods){
        this[methods]()
      },
      handleDetail(row){
        this.detailId = row.id
        this.detailOpenType = "detail"
        this.detailTitle="详情"
        this.detailDialogShow = true
      },
      handleExport(){
        let params = {
          ...this.searchData.form,
          checkType : 1,
          year:this.searchData.form.year
        }
        cityDiagnosisListExport(params,`城市诊断信息(自体检)_${new Date().getTime()}.xlsx`)
      },
      closeDialog(){
        this.detailDialogShow = false
      },
    },
    activated(){
      this.fetch({... this.searchData.form})
    },
    created(){
      // 获取字典值
      this.checkTypeOptions = []
      this.$commonJs.filterDataDict("cityCheckType",this.checkTypeOptions)
      this.resultTypeOptions = []
      this.$commonJs.filterDataDict("cityCheckResultType",this.resultTypeOptions)
      getAreaList({parentCode:"440000000000"}).then(res=>{
        if(res.code==200){
          this.cityList = res.data
        }
      })
      this.searchAll()
    },
    mounted(){
      this.tableMainHeight = document.getElementsByClassName('table-main')[0].clientHeight - 44 //减去顶部按钮列表的高度
    }
  }
</script>

<style lang="scss" scoped>
  .table-all {
    display: flex;
    flex-direction: column;
    height: 100%;

    .search-box {
      padding-bottom: 6px;
    }

    .table-box {
      padding-top: 10px;
      flex: 1;
      display: flex;
      flex-direction: column;

      .table-main {
        flex: 1;
        overflow: auto;
      }

      .table-page {
        // padding-top: 10 px;
        // padding-bottom: 10 px;
      }

      ::v-deep .pagination-container {
        text-align: right;
      }

    }
    .title{
      border-bottom: 1px solid #f1f1f1;
      padding-bottom: 5px;
      margin-bottom: 5px;
      margin-right: 10px;
    }
    .col{
      text-align: center;
      margin: 5px;
      padding: 5px;
    }
    .div_title{
      font-size: 16px;
      padding-bottom: 5px;
    }
    .div_num{
      font-size: 20px;
      font-weight: 900;
    }
  }
</style>
